package com.eqdd.yiqidian.ui.mine.renwu;

import android.content.Intent;
import android.databinding.DataBindingUtil;

import com.bigkoo.pickerview.TimePickerView;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.RenwuListSearchActivityCustom;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.RenwuSearchBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimTwoSectionEditBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.DateUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.jakewharton.rxbinding.view.RxView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class RenwuListSearchActivity extends BaseActivity {
    private RenwuListSearchActivityCustom dataBinding;
    String startDate = null;
    String endDate = null;
    String para = null;
    private TimePickerView pvEndTime;
    private TimePickerView pvStartTime;
    private SlimAdapterEx<RenwuSearchBean> slimAdapterEx;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_renwu_list_search);
        initTopTitleBar(View.VISIBLE, "搜索项目");
        initTopRightText("搜索", v -> {
            search();
        });
    }

    @Override
    public void initData() {
        initTime();
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_28, new SlimInjector<RenwuSearchBean>() {
            @Override
            public void onInject(RenwuSearchBean data, IViewInjector injector) {
                injector.text(R.id.tv_left_content, data.getTaskName())
                        .text(R.id.tv_right_hint_content, "任务编码" + data.getTaskCode());
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {

                    Intent intent = new Intent();
                    intent.putExtra("bean", slimAdapterEx.getDataItem(position));
                    setResult(Config.SUCCESS, intent);
                    finish();
                });
    }

    @Override
    public void setView() {

        RxView.clicks(dataBinding.tvUpperContent)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe(v -> {
                    if (!pvStartTime.isShowing()) {
                        pvStartTime.show();
                    }
                });
        RxView.clicks(dataBinding.tvDownContent)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe(v -> {
                    if (!pvEndTime.isShowing()) {
                        pvEndTime.show();
                    }
                });
        RxView.clicks(dataBinding.ivUpperClear)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe(v -> {
                    startDate=null;
                    dataBinding.tvUpperContent.setText("");
                    dataBinding.ivUpperClear.setVisibility(View.INVISIBLE);
                });
        RxView.clicks(dataBinding.ivDownClear)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe(v -> {
                    endDate=null;
                    dataBinding.tvDownContent.setText("");
                    dataBinding.ivDownClear.setVisibility(View.INVISIBLE);
                });
        search();
    }

    private void initTime() {
        Calendar now = Calendar.getInstance();
        now.setTime(new Date());
        pvStartTime = new TimePickerView.Builder(this, (date, v) -> {
            startDate = DateUtil.parse(date, "yyyy-MM-dd");
            dataBinding.tvUpperContent.setText(startDate);
            dataBinding.ivUpperClear.setVisibility(View.VISIBLE);

        })
                .setOutSideCancelable(true)
                .setType(new boolean[]{true, true, true, false, false, false})
                .setRange(2017, 2050)
                .setDate(now)
                .build();
        pvEndTime = new TimePickerView.Builder(this, (date, v) -> {
            endDate = DateUtil.parse(date, "yyyy-MM-dd");
            dataBinding.tvDownContent.setText(endDate);
            dataBinding.ivDownClear.setVisibility(View.VISIBLE);

        })
                .setOutSideCancelable(true)
                .setType(new boolean[]{true, true, true, false, false, false})
                .setRange(2017, 2050)
                .setDate(now)
                .build();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    private void search() {
        OkGo.<HttpResult<List<RenwuSearchBean>>>post(HttpConfig.BASE_URL + HttpConfig.SEARCH_RENWU)
                .params("companyId", user.getCompanyId())
                .params("startDate", startDate)
                .params("endDate", endDate)
                .params("para", dataBinding.searchView.getQuery().toString())
                .execute(new DialogCallBack<HttpResult<List<RenwuSearchBean>>>(RenwuListSearchActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<RenwuSearchBean>>> response) {
                        HttpResult<List<RenwuSearchBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.updateData(httpResult.getItems());
                        }
                    }
                });
    }
}
